package com.readbook.action;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.readbook.service.WdsjService;
import com.readbook.util.SecCheck;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/wdsj")
public class wdsj {
    @Resource
    private WdsjService wdsjService;

    /**
     * @author 牟明艳
     * @comments 正在阅读
     * @time 2019年8月29日09:48:01
     */
    @RequestMapping("getZayd.html")
    @ResponseBody
    public Map<String, Object> getZayd(HttpServletRequest request, HttpServletResponse response) {
        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        //教师推荐
        Map<String, Object> jstjmap = new HashMap<String, Object>();
        jstjmap.put("start", startn);
        jstjmap.put("end", endn);
        jstjmap.put("usid", usid);
        jstjmap.put("wwc", "1");
        jstjmap.put("sfsj", "0");
        List<Map<String, Object>> list1 = wdsjService.findBookPlanPage(jstjmap);
        int totalnum = wdsjService.countBookPlanPage(jstjmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        return rmap;
    }

    /**
     * @author 牟明艳
     * @comments 历史阅读
     * @time 2019年8月29日09:50:16
     */
    @RequestMapping("getLsyd.html")
    @ResponseBody
    public Map<String, Object> getLsyd(HttpServletRequest request, HttpServletResponse response) {
        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        //教师推荐
        Map<String, Object> jstjmap = new HashMap<String, Object>();
        jstjmap.put("start", startn);
        jstjmap.put("end", endn);
        jstjmap.put("usid", usid);
        jstjmap.put("wc", "1");

        List<Map<String, Object>> list1 = wdsjService.findBookPlanPage(jstjmap);
        int totalnum = wdsjService.countBookPlanPage(jstjmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        return rmap;
    }

    /**
     * @author 牟明艳
     * @comments 我的收藏
     * @time 2019年8月29日09:50:44
     */
    @RequestMapping("getCollection.html")
    @ResponseBody
    public Map<String, Object> getCollection(HttpServletRequest request, HttpServletResponse response) {

        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        //教师推荐
        Map<String, Object> jstjmap = new HashMap<String, Object>();
        jstjmap.put("start", startn + "");
        jstjmap.put("end", endn + "");
        jstjmap.put("usid", usid);

        List<Map<String, Object>> list1 = wdsjService.findCollectionPage(jstjmap);
        int totalnum = wdsjService.countCollectionPage(jstjmap);
        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 保存评论
     * @time 2019年8月29日17:27:09
     */
    @RequestMapping("sendDiscuss.html")
    @ResponseBody
    public Map<String, Object> sendDiscuss(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String bid = request.getParameter("bid");
        String discuss = request.getParameter("discuss");
        String usid = request.getParameter("usid");
        String username = request.getParameter("username");
        String pdisid = request.getParameter("pdisid");

        String disid = UUID.randomUUID().toString().replaceAll("-", "");
        smap.put("disid", disid);
        smap.put("bid", bid);
        smap.put("discuss", discuss);
        smap.put("usid", usid);
        smap.put("username", username);
        smap.put("pdisid", pdisid);
        smap.put("sh", "1");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        smap.put("discusstime", df.format(new Date()));


        //先判断是否有不符合的法律的数据
        String pjson= JSON.toJSONString(smap);
        Map<String,Object>pd=new HashMap<String,Object>();
        pd= SecCheck.msgSecCheck(pjson);
        if(pd.get("result").toString().equals("false")){
            return pd;
        }

        int i = wdsjService.sendDiscuss(smap);

        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            remap.put("msg", "评论成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "评论失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 点赞成功
     * @time 2019年8月31日10:58:28
     */
    @RequestMapping("discussthumbsupAdd.html")
    @ResponseBody
    public boolean discussthumbsupAdd(HttpServletRequest request, HttpServletResponse response) {
        String disid = request.getParameter("disid");
        String usid = request.getParameter("usid");
        Map<String, Object> smap = new HashMap<String, Object>();
        smap.put("disid", disid);
        smap.put("usid", usid);
        int i = wdsjService.discussthumbsupAdd(smap);
        if (i > 0) {
            return true;
        }
        return false;
    }


    /**
     * @author 牟明艳
     * @comments 点赞失败
     * @time 2019年8月31日10:58:28
     */
    @RequestMapping("discussthumbsupSub.html")
    @ResponseBody
    public boolean discussthumbsupSub(HttpServletRequest request, HttpServletResponse response) {
        String disid = request.getParameter("disid");
        String usid = request.getParameter("usid");
        Map<String, Object> smap = new HashMap<String, Object>();
        smap.put("disid", disid);
        smap.put("usid", usid);
        int i = wdsjService.discussthumbsupSub(smap);
        if (i > 0) {
            return true;
        }
        return false;
    }


    /**
     * @author 牟明艳
     * @comments 根据书籍ID查询讨论
     * @time 2019年8月25日00:48:53
     */
    @RequestMapping("moreDicussPlByDisid.html")
    @ResponseBody
    public List<Map<String, Object>> moreDicussPlByDisid(HttpServletRequest request, HttpServletResponse response) {
        String pdisid = request.getParameter("pdisid");
        int page = Integer.parseInt(request.getParameter("page"));
        int start = page * 10 - 10 + 1;
        int end = page * 10;

        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("start", start);
        getm.put("end", end);
        getm.put("disid", pdisid);
        List<Map<String, Object>> listm = new ArrayList<>();
        listm = wdsjService.moreDicussPlByDisid(getm);
        return listm;
    }


    /**
     * @author 牟明艳
     * @comments 检查用户是否已经填写心得体会
     * @time 2019年9月4日19:30:43
     */
    @RequestMapping("checkReadlearn.html")
    @ResponseBody
    public boolean checkReadlearn(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        List<Map<String, Object>> listm = new ArrayList<>();
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        listm = wdsjService.checkReadlearn(getm);
        if (listm.size() > 0) {
            return false;
        }
        return true;
    }


    /**
     * @author 牟明艳
     * @comments 提交心得体会
     * @time 2019年8月31日10:58:28
     */
    @RequestMapping("saveReadlearn.html")
    @ResponseBody
    public Map<String, Object> saveReadlearn(HttpServletRequest request, HttpServletResponse response) {
        String learncontent = request.getParameter("learncontent");
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        String username = request.getParameter("username");
        String learnid = UUID.randomUUID().toString().replaceAll("-", "");

        Map<String, Object> smap = new HashMap<String, Object>();
        smap.put("learnid", learnid);
        smap.put("usid", usid);
        smap.put("username", username);
        smap.put("learncontent", learncontent);
        smap.put("bid", bid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        smap.put("addtime", df.format(new Date()));
        smap.put("sh", "0");

        //先判断是否有不符合的法律的数据
        String pjson= JSON.toJSONString(smap);
        Map<String,Object>remap=new HashMap<String,Object>();
        remap= SecCheck.msgSecCheck(pjson);
        if(remap.get("result").toString().equals("false")){
            return remap;
        }

        int i = wdsjService.saveReadlearn(smap);
        if (i > 0) {
            //用户必须上传图片
            String urls = request.getParameter("upurls");//图片路径
            List<String> listr = (List<String>) JSONArray.parse(urls);

            for (int m = 0; m < listr.size(); m++) {
                String picurl = listr.get(m);
                String code = "readlearn";
                String codename = "xdth";

                Map<String, Object> imap = new HashMap<String, Object>();
                imap.put("picurl", picurl);
                imap.put("code", code);
                imap.put("codename", codename);
                imap.put("pid", learnid);
                imap.put("xh", m);
                int k = wdsjService.savePicture(imap);
                if (k <= 0) {
                    remap.put("msg", "心得体会发布失败!");
                    remap.put("result", false);
                    return remap;
                }
            }
            remap.put("msg", "心得体会发布成功!");
            remap.put("result", true);
            return remap;
        }
        remap.put("msg", "心得体会发布失败!");
        remap.put("result", false);
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 根据心得体会ID查询更多的评论
     * @time 2019年9月1日12:26:37
     */
    @RequestMapping("moreReadlearnPlByid.html")
    @ResponseBody
    public List<Map<String, Object>> moreReadlearnPlByid(HttpServletRequest request, HttpServletResponse response) {
        String learnid = request.getParameter("learnid");
        int page = Integer.parseInt(request.getParameter("page"));
        int start = page * 10 - 10 + 1;
        int end = page * 10;

        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("start", start);
        getm.put("end", end);
        getm.put("learnid", learnid);
        List<Map<String, Object>> listm = new ArrayList<>();
        listm = wdsjService.moreReadlearnPlByid(getm);
        return listm;
    }


    /**
     * @author 牟明艳
     * @comments 检查心得体会评论
     * @time 2019年9月4日19:01:44
     */
    @RequestMapping("checkReacLearnpl.html")
    @ResponseBody
    public boolean checkReacLearnpl(HttpServletRequest request, HttpServletResponse response) {
        String learnid = request.getParameter("learnid");
        String usid = request.getParameter("usid");
        Map<String, Object> smap = new HashMap<String, Object>();
        smap.put("learnid", learnid);
        smap.put("usid", usid);
        List<Map<String, Object>> list1 = wdsjService.checkReacLearnpl(smap);
        if (list1.size() > 0) {
            return false;
        }
        return true;
    }


    /**
     * @author 牟明艳
     * @comments 提交心得体会评论
     * @time 2019年9月1日15:23:25
     */
    @RequestMapping("saveReadlearnpl.html")
    @ResponseBody
    public boolean saveReadlearnpl(HttpServletRequest request, HttpServletResponse response) {
        String learnid = request.getParameter("learnid");
        String plcontent = request.getParameter("plcontent");
        String usid = request.getParameter("usid");
        String username = request.getParameter("username");
        String pstar = request.getParameter("pstar");
        String jf = request.getParameter("jf");

        Map<String, Object> smap = new HashMap<String, Object>();
        smap.put("learnid", learnid);
        smap.put("plcontent", plcontent);
        smap.put("usid", usid);
        smap.put("username", username);
        smap.put("pstar", pstar);
        smap.put("jf", jf);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String addtime = df.format(new Date());
        smap.put("addtime", addtime);

        int i = wdsjService.saveReadlearnpl(smap);
        if (i > 0) {
            //查询被评论人的usid与usname
            List<Map<String, Object>> list1 = wdsjService.listReadLearnBylearnid(smap);
            if (list1.size() > 0) {
                //记录至积分记录文件
                Map<String, Object> jfmap = new HashMap<String, Object>();
                String jfid = UUID.randomUUID().toString().replaceAll("-", "");
                jfmap.put("jfid", jfid);
                jfmap.put("lyid", learnid);
                jfmap.put("jfly", "readlearnpl");//积分来源 打卡
                jfmap.put("addtime", addtime);
                jfmap.put("usid", list1.get(0).get("USID"));
                jfmap.put("username", list1.get(0).get("USERNAME"));
                jfmap.put("jfnum", jf);
                wdsjService.savejfjl(jfmap);
                return true;
            }
        }
        return false;
    }


    /**
     * @author 牟明艳
     * @comments 查询读书打卡记录
     * @time 2019年9月3日21:00:14
     */
    @RequestMapping("getBookreadLog.html")
    @ResponseBody
    public Map<String, Object> getBookreadLog(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        Map<String, Object> setm = new HashMap<>();
        setm = wdsjService.getBookreadLog(getm);
        return setm;
    }


    /**
     * @author 牟明艳
     * @comments 记录读书日志
     * @time 2019年9月4日12:19:23
     */
    @RequestMapping("savereadlog.html")
    @ResponseBody
    public Map<String,Object> savereadlog(HttpServletRequest request, HttpServletResponse response) {
        String planid = request.getParameter("planid");//计划ID
        String bid = request.getParameter("bid");//书本ID
        String pagenum = request.getParameter("pagenum");//今天的阅读量

        String goodsentence = "";//好词好句好段
        if (request.getParameter("goodsentence") != null) {
            goodsentence = request.getParameter("goodsentence");//好词好句好段
        }

        String reaction = "";//章节读后感
        if (request.getParameter("reaction") != null) {
            reaction = request.getParameter("reaction");//好词好句好段
        }

        String usid = request.getParameter("usid");//用户ID
        String username = request.getParameter("username");//用户名
        String totalpage = request.getParameter("totalpage");//书的总页数
        String minnum = request.getParameter("minnum");//每天最低读书量
        String totalyd = request.getParameter("totalyd");//之前的阅读总页数

        Map<String, Object> smap = new HashMap<String, Object>();
        String logid = UUID.randomUUID().toString().replaceAll("-", "");
        smap.put("logid", logid);
        smap.put("planid", planid);
        smap.put("bid", bid);
        smap.put("pagenum", pagenum);
        smap.put("goodsentence", goodsentence);
        smap.put("reaction", reaction);
        smap.put("usid", usid);
        smap.put("username", username);

        //先判断是否有不符合的法律的数据
        String pjson= JSON.toJSONString(smap);
        Map<String,Object>pd=new HashMap<String,Object>();
        pd= SecCheck.msgSecCheck(pjson);
        if(pd.get("result").toString().equals("false")){
            return pd;
        }
        Map<String,Object>fmap=new HashMap<>();//返回记录

        //计算进度(当前的与总页数进行比较)
        int totalpage1 = Integer.parseInt(totalpage);
        int pagenum1 = Integer.parseInt(pagenum);
        double c = (float) pagenum1 / (float) totalpage1 * 100;
        int progress = (int) Math.ceil(c);
        smap.put("progress", progress);


        //计算总进度
        int jd = 0;
        int totalnum = Integer.parseInt(totalyd) + Integer.parseInt(pagenum);
        if (totalnum >= totalpage1) {
            jd = 100;
        } else {
            double jindu = (float) totalnum / (float) totalpage1 * 100;
            jd = (int) Math.ceil(jindu);
        }


        //.........................计算所得积分start..........................
        //整本书读完得20分，完成80%得16分 如果阅读数量超过了计划 则有积分
        int getjf = 0;
        //永远判断是否有积分
        boolean checkjf = false;
        //1.查询是否超过预期
        Map<String, Object> todaymap = new HashMap<String, Object>();
        SimpleDateFormat nyr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String today = nyr.format(new Date());
        todaymap.put("today", today);//计划ID
        todaymap.put("planid", planid);//计划ID
        //计算最少要在多少天完成
        float zddsl = Float.parseFloat(minnum);
        double tianshu = (float) totalpage1 / zddsl;
        int jhts = (int) Math.ceil(tianshu);
        todaymap.put("jhts", jhts);//计划天数

        String yqn = wdsjService.listTimeCq(todaymap);
        if (yqn.equals("1")) {//没超过预期
            //2.计算是否完成了全部
            if (jd < 100) {
                //没有完成全部
                if (Integer.parseInt(minnum) <= Integer.parseInt(pagenum)) {
                    //是否达到每日标注
                    checkjf = true;
                    double j = ((float) pagenum1 / (float) totalpage1) * 20;
                    getjf = (int) Math.round(j);
                }
            } else {
                //完成了全部获取剩余全部积分
                getjf = wdsjService.getSyjfByLogid(todaymap);
                checkjf = true;
            }
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String addtime = df.format(new Date());
        //用户获取积分的条件
        if (checkjf) {
            smap.put("getjf", getjf);
            //记录至积分记录文件
            Map<String, Object> jfmap = new HashMap<String, Object>();
            String jfid = UUID.randomUUID().toString().replaceAll("-", "");
            jfmap.put("jfid", jfid);
            jfmap.put("lyid", logid);
            jfmap.put("jfly", "readlog");//积分来源 打卡
            jfmap.put("addtime", addtime);
            jfmap.put("usid", usid);
            jfmap.put("username", username);
            jfmap.put("jfnum", getjf);
            wdsjService.savejfjl(jfmap);
        }
        //.........................计算所得积分end..........................


        Calendar calendar = Calendar.getInstance();
        smap.put("year", calendar.get(Calendar.YEAR));
        smap.put("month", calendar.get(Calendar.MONTH) + 1);
        smap.put("day", calendar.get(Calendar.DATE));
        smap.put("addtime", addtime);
        int i = wdsjService.savereadlog(smap);

        //保存阅读剪影
        if (i > 0) {
            //上传阅读剪影
            String urls = request.getParameter("upurls");//图片路径
            List<String> listr = (List<String>) JSONArray.parse(urls);
            for (int m = 0; m < listr.size(); m++) {
                String picurl = listr.get(m);
                String code = "readlog";
                String codename = "ydjy";

                Map<String, Object> imap = new HashMap<String, Object>();
                imap.put("picurl", picurl);
                imap.put("code", code);
                imap.put("codename", codename);
                imap.put("pid", logid);
                imap.put("xh", m);
                int k = wdsjService.savePicture(imap);
                if (k <= 0) {
                    fmap.put("msg","阅读剪影图片保存失败");
                    fmap.put("result",false);
                    return fmap;
                }
            }

            //上传好词好句图片
            String hchjurls = request.getParameter("hchjurls");//图片路径
            List<String> lishchjhd = (List<String>) JSONArray.parse(hchjurls);
            for (int n = 0; n < lishchjhd.size(); n++) {
                String picurl = lishchjhd.get(n);
                String code = "readlog";
                String codename = "hchjhd";

                Map<String, Object> imap = new HashMap<String, Object>();
                imap.put("picurl", picurl);
                imap.put("code", code);
                imap.put("codename", codename);
                imap.put("pid", logid);
                imap.put("xh", n);
                int k = wdsjService.savePicture(imap);
                if (k <= 0) {
                    fmap.put("msg","好词好句图片保存失败");
                    fmap.put("result",false);
                    return fmap;
                }
            }


            //章节读后感图片
            String zjdhgurls = request.getParameter("zjdhgurls");//图片路径
            List<String> liszjdhg = (List<String>) JSONArray.parse(zjdhgurls);
            for (int z = 0; z < liszjdhg.size(); z++) {
                String picurl = liszjdhg.get(z);
                String code = "readlog";
                String codename = "zjdhg";

                Map<String, Object> imap = new HashMap<String, Object>();
                imap.put("picurl", picurl);
                imap.put("code", code);
                imap.put("codename", codename);
                imap.put("pid", logid);
                imap.put("xh", z);
                int k = wdsjService.savePicture(imap);
                if (k <= 0) {
                    fmap.put("msg","章节读后感图片保存失败");
                    fmap.put("result",false);
                    return fmap;
                }
            }

            //修改读书计划的总进度
            smap.put("jd", jd);
            wdsjService.updatebookplanJd(smap);
            fmap.put("msg","提交成功");
            fmap.put("result",true);
            return fmap;
        }
        fmap.put("msg","数据保存失败");
        fmap.put("result",false);
        return fmap;
    }


    /**
     * @author 牟明艳
     * @comments 查询当天的读书打卡记录
     * @time 2019年9月3日21:00:14
     */
    @RequestMapping("getBookreadLogToday.html")
    @ResponseBody
    public boolean getBookreadLogToday(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        Calendar calendar = Calendar.getInstance();
        getm.put("year", calendar.get(Calendar.YEAR));
        getm.put("month", calendar.get(Calendar.MONTH) + 1);
        getm.put("day", calendar.get(Calendar.DATE));
        List<Map<String, Object>> list1 = wdsjService.getBookreadLogToday(getm);
        if (list1.size() > 0) {
            return false;
        }
        return true;
    }


}
